package com.hushan.elevator.service.impl;

import com.hushan.elevator.dao.RoleRepository;
import com.hushan.elevator.model.Role;
import com.hushan.elevator.service.RoleService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: zzy
 * @Date: 2024/10/22 23:40
 * @Title:
 * @Version:
 * @Package: com.hushan.elevator.service.impl
 */
@Service
public class RoleServiceImpl implements RoleService {

    private final RoleRepository roleDao;

    @Autowired
    public RoleServiceImpl(RoleRepository roleRepository) {
        this.roleDao = roleRepository;
    }

    @Override
    public List<Role> findAllRoles() {
        return roleDao.findAll();
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findById(id).orElse(null);
    }


    @Override
    public List<Role> findRolesByCreatedBy(Integer createdBy) {
        return roleDao.findByCreatedBy(createdBy);
    }

    @Override
    public List<Role> findRolesByDeleted(Boolean deleted) {
        return roleDao.findByIsDeleted(deleted);
    }

    @Override
    public List<Role> findRolesByCreatedTimeBetween(LocalDateTime start, LocalDateTime end) {
        return roleDao.findByCreatedTimeBetween(start, end);
    }

    @Override
    public List<Role> findRolesByUpdatedTimeBetween(LocalDateTime start, LocalDateTime end) {
        return roleDao.findByUpdatedTimeBetween(start, end);
    }

    @Override
    @Transactional
    public void saveRole(Role role) {
        roleDao.save(role);
    }

    @Override
    @Transactional
    public void deleteRole(Integer id) {
        roleDao.deleteById(id);
    }

    @Override
    public Role findRoleByUser(Integer userId) {
        return roleDao.findRoleByUser(userId);
    }
}